package com.haoting.blogservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.haoting.blogservice.entity.CommentSupport;
import com.haoting.blogservice.entity.Support;
import com.haoting.blogservice.service.CommentSupportService;
import com.haoting.commonutils.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tingting
 * @since 2021-02-04
 */
@RestController
@RequestMapping("/blogservice/commentSupport")
//@CrossOrigin
public class CommentSupportController {

    @Autowired
    CommentSupportService commentSupportService;

    @PostMapping("supportblog")
    public CommonResult supportblog(@RequestBody CommentSupport support){

        commentSupportService.save(support);

        return CommonResult.ok();
    }

    @PostMapping("supportor")
    public CommonResult supportor(@RequestBody CommentSupport support){
        QueryWrapper<CommentSupport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id",support.getCommentId())
                .eq("consumer_id",support.getConsumerId());
        int count = commentSupportService.count(queryWrapper);
        if(count>=1){
            return CommonResult.ok().data("exist","true");
        }else {
            return CommonResult.error().data("exist","false");
        }

    }


    @DeleteMapping("delsupport")
    public CommonResult delsupport(@RequestBody CommentSupport support){

        QueryWrapper<CommentSupport> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("comment_id",support.getCommentId())
                .eq("consumer_id",support.getConsumerId());
        commentSupportService.remove(queryWrapper);
        return CommonResult.ok();
    }


    @PostMapping("updateSupportCount/{commentId}")
    public CommonResult addSupportCount(@PathVariable String commentId){
        commentSupportService.updateSupportCount(commentId);
        return CommonResult.ok();
    }
}

